import de.motsangria.enums.ClubFunction

class Profile {

  static belongsTo = [User]

  static mapping = {
      cache true
  }
  
  //required properties
  User user

  //Personal add-on properties
  byte[] photo
  String firstName
  String secondName
  String lastName
  Date birthday
  ClubFunction clubFunction = ClubFunction.Member
  String position
  String company
  String interests

  //Messenger add-on properties
  String jabberId
  String twitterId
  String facebookId
  String xingId
  String linkedinId

  //Private contact data add-on properties
  String privateStreet
  String privatePostCode
  String privatePlace
  String privateState
  String privateCountry
  String privateWebSite
  String privatePictureAlbum
  String privatePhone
  String privateMobile
  String privateFax

  //business contact data add-on properties
  String businessStreet
  String businessPostCode
  String businessPlace
  String businessState
  String businessCountry
  String businessWebSite
  String businessPhone
  String businessMobile
  String businessFax

  //statistical properties
  Date dateCreated = new Date()
  Date lastUpdated = new Date()
  Date lastLogin
  Date lastAction
  String lastMedium

  static constraints = {
    //constraints for required properties
    user(nullable: false)

    //constraints for optional properties
    photo(nullable: true, maxSize: 65535)
    firstName(nullable: true)
    secondName(nullable: true)
    lastName(nullable: true)
    birthday(nullable: true)
    clubFunction(nullable: false)
    position(nullable: true)
    company(nullable: true)
    interests(nullable: true)

    //Messenger add-on properties
    jabberId(nullable: true, unique: true)
    twitterId(nullable: true, unique:true)
    facebookId(nullable: true, unique:true)
    xingId(nullable: true, unique:true)
    linkedinId(nullable: true, unique:true)

    //Private contact data add-on properties
    privateStreet(nullable: true)
    privatePostCode(nullable: true)
    privatePlace(nullable: true)
    privateState(nullable: true)
    privateCountry(nullable: true)
    privateWebSite(nullable: true, url:true)
    privatePictureAlbum(nullable: true, url:true)
    privatePhone(nullable: true)
    privateMobile(nullable: true)
    privateFax(nullable: true)

    //business contact data add-on properties
    businessStreet(nullable: true)
    businessPostCode(nullable: true)
    businessPlace(nullable: true)
    businessState(nullable: true)
    businessCountry(nullable: true)
    businessWebSite(nullable: true, url:true)
    businessPhone(nullable: true)
    businessMobile(nullable: true)
    businessFax(nullable: true)

    //constraints for statistical properties
    dateCreated(nullable: false)
    lastUpdated(nullable: false)
    lastLogin(nullable: true)
    lastAction(nullable: true)
    lastMedium(nullable: true, maxSize: 128)
  }

  def afterUpdate = {
    if (user != null) {
      new Message(user: user, content: "$user hat sein Benutzerprofil aktualisiert.", medium: "MoTS Update Notifier").save()
      log.info "User profile of user $user updated"
    }
  }

  public String toString() {
    if (!firstName && !lastName) {
      return user.username
    } else {
      return "${firstName} ${lastName}"
    }
  }
}
